package br.com.skytef.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class ConnectionFactory {
	public static Connection getConnectionTomcat(boolean tomcat)
			throws Exception {

		InitialContext context = new InitialContext();
		DataSource ds = (DataSource) context
				.lookup("java:comp/env/jdbc/navs_vend_prod");
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			throw new Exception(e.getMessage());
		}
	}

	public static Connection getConnections(boolean localDataBase) {
		String url;
		String password;
		String dbName;
		String driver;
		String userName;

		try {

			Connection conn = null;
			if (localDataBase == true) {
				// Para conexao Local
				url = "jdbc:mysql://127.0.0.1:3306/"; 
				password = "admin";                   
			} else {
				// Para conexao no servidor
				password = "qwe123";                     
				url = "jdbc:mysql://192.168.0.70:3306/"; 
			}

			dbName = "navs_vend_prod?allowMultiQueries=true";
			driver = "com.mysql.jdbc.Driver";
			userName = "root";

			DriverManager.registerDriver(new com.mysql.jdbc.Driver());
			conn = DriverManager.getConnection(url + dbName, userName, password);

			return conn;

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

	}

	public static Connection getConnection() {
		return getConnections(false);
	}
}
